﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GraphDrawer.GraphProvider;

namespace GraphDrawer {
	public partial class SetupSimulation : UserControl {
		private WizardMenu m_menu;
		private SimulationSetupSL m_simulationSetup = new SimulationSetupSL();
		private StopConditionSL maxSteps = new StopConditionSL();
		private StateChangePlanSL plan;
		private static RoutedEventHandler popUpOk;
		private ChangePlanQuest quest = new ChangePlanQuest();

		public event RoutedEventHandler isSetupFinished;

		public SimulationSetupSL SimulationSetup {
			get {
				return m_simulationSetup;
			}
			set {
				m_simulationSetup = value;
			}
		}

		public SetupSimulation(WizardMenu menu) {
			InitializeComponent();

			m_simulationSetup.stopConditionsSL = new System.Collections.ObjectModel.ObservableCollection<StopConditionSL>();
			m_simulationSetup.stopConditionsSL.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(stopConditionsSL_CollectionChanged);
			m_simulationSetup.stateChangePlansSL = new System.Collections.ObjectModel.ObservableCollection<StateChangePlanSL>();
			m_simulationSetup.stateChangePlansSL.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(stateChangePlansSL_CollectionChanged);
			m_simulationSetup.maxContactsPerStep = 1;

			maxSteps.initParams = new ArrayOfAnyType();
			maxSteps.stopConditionName = "EpidemicSimulator.stopconditions.MaxSimStepNumberStopCondition";
			m_simulationSetup.stopConditionsSL.Add(maxSteps);

			maxSteps.initParams.Clear();
			if (!String.IsNullOrEmpty(Steps.Text))
				maxSteps.initParams.Add(Convert.ToInt32(Steps.Text));

			m_menu = menu;
			isSetupFinished += new RoutedEventHandler(setupSimulation_isSetupFinished);
		}

		void setupSimulation_isSetupFinished(object sender, RoutedEventArgs e) {

		}

		void stopConditionsSL_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
			//throw new NotImplementedException();
		}

		void stateChangePlansSL_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
			//throw new NotImplementedException();
		}

		public void Init() {
			//string[] states = m_menu.GetAvailableStates();
			//if (states.Length > 0)
			//    statesLabel.Visibility = Visibility.Visible;
			//foreach (var item in states) {
			//    statesNamesPanel.Children.Add(new Label() {
			//        Content = item,
			//        Height = 20,
			//        VerticalAlignment = VerticalAlignment.Center,
			//        Margin = new Thickness(0, 3, 0, 0)
			//    });

			//    statesCountPanel.Children.Add(new TextBox() {
			//        Width = 50,
			//        Height = 20,
			//        Margin = new Thickness(0, 3, 0, 0)
			//    });
			//}

			strategies.Children.Clear();

			WizardMenu.graphProvider.GetAvailableStateChangeStrategiesCompleted +=
				new EventHandler<GraphProvider.GetAvailableStateChangeStrategiesCompletedEventArgs>
					(graphProvider_GetAvailableStateChangeStrategiesCompleted);
			WizardMenu.graphProvider.GetAvailableStateChangeStrategiesAsync();
		}

		void graphProvider_GetAvailableStateChangeStrategiesCompleted(object sender, GraphProvider.GetAvailableStateChangeStrategiesCompletedEventArgs e) {
			inculationChooser.Items.Clear();
			foreach (var item in e.Result)
				inculationChooser.Items.Add(item);
		}

		private void Steps_TextChanged(object sender, TextChangedEventArgs e) {
			maxSteps.initParams.Clear();
			maxSteps.initParams.Add(Convert.ToInt32(
				Steps.Text.Length > 0 ?
				Steps.Text : "0"
			));
		}

		private void vaccines_TextChanged(object sender, TextChangedEventArgs e) {

		}

		private void inculationChooser_SelectionChanged(object sender, SelectionChangedEventArgs e) {
			btnChangePlan.IsEnabled = true;
		}

		private void btnChangePlan_Click(object sender, RoutedEventArgs e) {
			plan = new StateChangePlanSL();
			plan.stateChangeStrategyName = "EpidemicSimulator.strategies." + inculationChooser.SelectedItem.ToString();

			App.Current.Host.Content.IsFullScreen = false;

			popUpOk = new RoutedEventHandler(PopUp_OK);
			WizardMenu.PopUp.OK += popUpOk;
			WizardMenu.PopUp.AddUserControl(quest);
			quest.Init();
			WizardMenu.PopUp.Visibility = Visibility.Visible;
		}

		void PopUp_OK(object sender, RoutedEventArgs e) {
			WizardMenu.PopUp.OK -= popUpOk;
			plan.inPersents = quest.Percentage.IsChecked.Value;
			plan.value = Convert.ToInt32(quest.Val.Text);
			m_simulationSetup.stateChangePlansSL.Add(plan);
			strategies.Children.Add(new Label() {
				Content = (strategies.Children.Count + 1).ToString() + ". "
					+ inculationChooser.SelectedItem.ToString() + ": "
					+ plan.value + " %",
				Margin = new Thickness(0, 3, 0, 0)
			});
		}
	}
}
